//
//  RPBaseAnimation.m
//  RPAlertControllerDemo
//
//  Created by iOS on 21/11/22.
//  Copyright (c) 2021年 sword. All rights reserved.
//

#import "RPBaseAnimation.h"
//#import "MacrosDefine.h"
#define IOSScreenWidth          ([UIScreen mainScreen].bounds.size.width)
#define IOSScreenHeight         ([UIScreen mainScreen].bounds.size.height)

/**
 动画父类
 */
@interface RPBaseAnimation ()
@property(nonatomic, assign) BOOL isPresenting;
@end

@implementation RPBaseAnimation

- (instancetype)initWithIsPresenting:(BOOL)isPresenting {
    if (self = [super init]) {
        self.isPresenting = isPresenting;
    }
    return self;
}

+ (instancetype)alertAnimationIsPresenting:(BOOL)isPresenting {
    return [[self alloc] initWithIsPresenting:isPresenting];
}

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.35;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (_isPresenting) {
        [self presentAnimateTransition:transitionContext];
    } else {
        [self dismissAnimateTransition:transitionContext];
    }
}

#pragma mark - 子类重写

- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
}

- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
}

@end

/**
 无动画(支持提示框,表格,侧滑)
 */
@implementation RPAlertNoneAnimation

- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController<RPAlertDelegate> *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    RPAlertController *alertController = (RPAlertController *)toVc.alertController;
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVc.view];
    
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:interval animations:nil completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:interval animations:nil completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0;
}

@end

/**
 普通动画(支持提示框,表格,侧滑)
 */
@implementation RPAlertNormalAnimation

- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController<RPAlertDelegate> *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    RPAlertController *alertController = (RPAlertController *)toVc.alertController;
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVc.view];
    CGFloat alertViewHeight = CGRectGetHeight(alertController.alertView.frame);
    // 动画前设置
    alertController.backgroundView.alpha = 0.0;
    switch (alertController.preferredStyle) {
        case RPAlertControllerStyleAlert:
            alertController.alertView.transform = CGAffineTransformMakeScale(0.1, 0.1);
            break;
        case RPAlertControllerStyleActionSheet:{
            if(alertController.useEasingfunction) {
                alertController.alertView.frame = CGRectMake(0, IOSScreenHeight, IOSScreenWidth, alertViewHeight);
            }else{
                alertController.alertView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(alertController.alertView.frame));
            }
        }
            break;
        case RPAlertControllerStyleSideSlipLeft:
            alertController.alertView.transform = CGAffineTransformMakeTranslation(-CGRectGetWidth(alertController.alertView.frame), 0);
            break;
        case RPAlertControllerStyleSideSlipRight:
            alertController.alertView.transform = CGAffineTransformMakeTranslation(CGRectGetMaxX(alertController.alertView.frame), 0);
            break;
    }
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    if(alertController.useEasingfunction) {
        interval = 1;
        [UIView animateWithDuration:interval
                              delay:0
             usingSpringWithDamping:0.8
              initialSpringVelocity:0.2
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
            // 动画设置
            alertController.backgroundView.alpha = 1.0;
            alertController.alertView.frame = CGRectMake(0, IOSScreenHeight - alertViewHeight , IOSScreenWidth, alertViewHeight);
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }else{
        [UIView animateWithDuration:interval animations:^{
            // 动画设置
            alertController.backgroundView.alpha = 1.0;
            alertController.alertView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
}

- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController<RPAlertDelegate> *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    RPAlertController *alertController = (RPAlertController *)fromVc.alertController;
    
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:interval animations:^{
        alertController.backgroundView.alpha = 0.0;
        
        switch (alertController.preferredStyle)
            case RPAlertControllerStyleAlert: {
                alertController.alertView.transform = CGAffineTransformMakeScale(0.1, 0.1);
                break;
            case RPAlertControllerStyleActionSheet:
                alertController.alertView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(alertController.alertView.frame));
                break;
            case RPAlertControllerStyleSideSlipLeft:
                alertController.alertView.transform = CGAffineTransformMakeTranslation(-CGRectGetWidth(alertController.alertView.frame), 0);
                break;
            case RPAlertControllerStyleSideSlipRight:
                alertController.alertView.transform = CGAffineTransformMakeTranslation(CGRectGetMaxX(alertController.alertView.frame), 0);
                break;
            }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

@end

/**
 普通动画增加弹跳效果(支持提示框,表格)
 */
@implementation RPAlertSpringAnimation

- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController<RPAlertDelegate> *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    RPAlertController *alertController = (RPAlertController *)toVc.alertController;
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVc.view];
    
    // 动画前设置
    alertController.backgroundView.alpha = 0.0;
    switch (alertController.preferredStyle) {
        case RPAlertControllerStyleAlert:
            alertController.alertView.transform = CGAffineTransformMakeScale(0.5, 0.5);
            break;
        case RPAlertControllerStyleActionSheet:
            alertController.alertView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(alertController.alertView.frame));
            break;
        default:
            break;
    }
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:interval animations:^{
        // 动画设置
        alertController.backgroundView.alpha = 1.0;
        switch (alertController.preferredStyle) {
            case RPAlertControllerStyleAlert:
                alertController.alertView.transform = CGAffineTransformMakeScale(1.05, 1.05);
                break;
            case RPAlertControllerStyleActionSheet:
                alertController.alertView.transform = CGAffineTransformMakeTranslation(0, -10.0);
                break;
            default:
                NSAssert(NO, @"%@ don't support preferredStyle = %ld",NSStringFromClass(self.class), alertController.preferredStyle);
                break;
        }
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:interval animations:^{
            alertController.alertView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }];
}

- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController<RPAlertDelegate> *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    RPAlertController *alertController = (RPAlertController *)fromVc.alertController;
    
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:interval animations:^{
        alertController.backgroundView.alpha = 0.0;
        
        switch (alertController.preferredStyle) {
            case RPAlertControllerStyleAlert:
                alertController.alertView.transform = CGAffineTransformMakeScale(0.9, 0.9);
                break;
            case RPAlertControllerStyleActionSheet:
                alertController.alertView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(alertController.alertView.frame));
                break;
            default:
                NSAssert(NO, @"%@ don't support preferredStyle = %ld",NSStringFromClass(self.class), alertController.preferredStyle);
                break;
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

@end

/**
 掉落动画(仅支持提示框)
 */
@implementation RPAlertDropDownAnimation

- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController<RPAlertDelegate> *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    RPAlertController *alertController = (RPAlertController *)toVc.alertController;
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVc.view];
    
    // 动画前设置
    alertController.backgroundView.alpha = 0.0;
    switch (alertController.preferredStyle) {
        case RPAlertControllerStyleAlert:
            alertController.alertView.transform = CGAffineTransformMakeTranslation(0, -CGRectGetMaxY(alertController.alertView.frame));
            break;
        default:
            break;
    }
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:interval delay:interval usingSpringWithDamping:0.65 initialSpringVelocity:0.5 options:0 animations:^{
        // 动画设置
        alertController.backgroundView.alpha = 1.0;
        switch (alertController.preferredStyle) {
            case RPAlertControllerStyleAlert:
                alertController.alertView.transform = CGAffineTransformIdentity;
                break;
            default:
                NSAssert(NO, @"%@ don't support preferredStyle = %ld",NSStringFromClass(self.class), alertController.preferredStyle);
                break;
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController<RPAlertDelegate> *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    RPAlertController *alertController = (RPAlertController *)fromVc.alertController;
    
    NSTimeInterval interval = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:interval animations:^{
        alertController.backgroundView.alpha = 0.0;
        
        switch (alertController.preferredStyle) {
            case RPAlertControllerStyleAlert:
                alertController.alertView.transform = CGAffineTransformMakeScale(0.9, 0.9);
                break;
            default:
                NSAssert(NO, @"%@ don't support preferredStyle = %ld",NSStringFromClass(self.class), alertController.preferredStyle);
                break;
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

@end
